package com.broad.commerce.code.modules;

import com.broad.commerce.code.modules.jdbc.EntityModel;
import com.broad.commerce.code.modules.jdbc.EntityModule;
import com.broad.commerce.code.modules.jdbc.FastMybatisModule;
import com.broad.commerce.code.modules.jdbc.JpaModule;
import com.broad.commerce.code.modules.jdbc.MapperModule;
import com.broad.commerce.code.modules.jdbc.MybatisModule;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;

/**
 * @author stephen
 * @version V1.0
 * @date 2022-02-08 13:43
 * @description: JdbcModule
 **/
@SuppressWarnings("DuplicatedCode")
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Builder
@Getter
@Accessors(chain = true)
public class JdbcModule {
	@Setter
	private String modelPackage;
	@Setter
	private String jpaPackage;
	@Setter
	private String fastMybatisPackage;
	@Setter
	private String mybatisPackage;
	@Setter
	private EntityModule entityModule;

	private EntityModel entityModel;
	private JpaModule jpaModule;
	private MybatisModule mybatisModule;
	private MapperModule mapperModule;
	private FastMybatisModule fastMybatisModule;

	public void initJdbcModule() {
		this.setEntityModel(entityModule.getUpdate());
		this.setJpaModule(entityModule.getUpdate());
		this.setMybatisModule(entityModule.getUpdate());
		this.setMapperModule(entityModule.getUpdate());
		this.setFastMybatisModule(entityModule.getUpdate());
	}

	private void setEntityModel(boolean update) {
		EntityModel entityModel = new EntityModel();
		entityModel.setUpdate(update);
		entityModel.setPackageName(this.modelPackage);
		entityModel.setClassName(this.entityModule.getClassName() + "DTO");
		entityModel.setEntityFields(this.entityModule.getEntityFields());
		this.entityModel = entityModel;
	}

	private void setJpaModule(boolean update) {
		JpaModule jpaModule = new JpaModule();
		jpaModule.setUpdate(update);
		jpaModule.setPackageName(this.jpaPackage);
		jpaModule.setClassName(this.entityModule.getClassName() + "Dao");
		jpaModule.setEntityClassName(this.entityModule.getClassName());
		jpaModule.setEntityFullClassName(this.entityModule.getFullClassName());
		this.jpaModule = jpaModule;
	}

	private void setMybatisModule(boolean update) {
		MybatisModule mybatisModule = new MybatisModule();
		mybatisModule.setUpdate(update);
		mybatisModule.setPackageName(this.mybatisPackage);
		mybatisModule.setClassName(this.entityModule.getClassName() + "Mapper");
		mybatisModule.setEntityClassName(this.entityModule.getClassName());
		mybatisModule.setEntityFullClassName(this.entityModule.getFullClassName());
		this.mybatisModule = mybatisModule;
	}

	private void setMapperModule(boolean update) {
		MapperModule mapperModule = new MapperModule();
		mapperModule.setUpdate(update);
		mapperModule.setClassName(this.mybatisModule.getClassName());
		mapperModule.setTableName(this.entityModule.getTableName());
		mapperModule.setEntityFields(this.entityModule.getEntityFields());
		mapperModule.setEntityClassName(this.entityModule.getClassName());
		mapperModule.setEntityFullClassName(this.entityModule.getFullClassName());
		mapperModule.setMapperFullClassName(this.mybatisModule.getFullClassName());
		this.mapperModule = mapperModule;
	}

	private void setFastMybatisModule(boolean update) {
		FastMybatisModule fastMybatisModule = new FastMybatisModule();
		fastMybatisModule.setUpdate(update);
		fastMybatisModule.setPackageName(this.fastMybatisPackage);
		fastMybatisModule.setClassName("T" + this.entityModule.getClassName() + "Mapper");
		fastMybatisModule.setEntityClassName(this.entityModule.getClassName());
		fastMybatisModule.setEntityFullClassName(this.entityModule.getFullClassName());
		this.fastMybatisModule = fastMybatisModule;
	}
}
